const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
function shouquan(){
  wx.showModal({
    title: '警告',
    content: '您点击了拒绝授权,将无法正常显示个人信息,点击确定重新获取授权。',
    success: function (res) {
      if (res.confirm) {
        wx.openSetting({
          success: (res) => {
            
            
            if (res.authSetting["scope.userInfo"] === true) {
              wx.getUserInfo({
                success: function (res) {
                  var code = wx.getStorageSync('codes');
                  var userInfo = res.userInfo;
                  var rawData = res['rawData'];
                  var signature = res['signature'];
                  var encryptData = res['encryptData'];
                  var encryptedData = res['encryptedData']; //注意是encryptedData不是encryptData...坑啊
                  var iv = res['iv'];
                  wx.request({
                    url: "https://api.chaotingtiyu.com/yuekeadmin/public/index.php/api/index/wx_login",
                    data: {
                      "code": code,
                      "rawData": rawData,
                      "signature": signature,
                      "encryptData": encryptData,
                      'iv': iv,
                      'encryptedData': encryptedData
                    },
                    success: function (res) {
                      // console.log(res)

                      wx.setStorageSync('openIds', res.data.openId);
                      wx.setStorageSync('session3rds', res.data.session3rd);
                      wx.setStorageSync('phone', res.data.phone);
                      wx.setStorageSync('uid',res.data.uid)
                      if (res.statusCode != 200) {
                        wx.showModal({
                          title: '登录失败'
                        });
                      }
                    },
                    complete: function () {
                     
                    }
                  });
                }
              })
            }
            /*
             * res.authSetting = {
             *   "scope.userInfo": true,
             *   "scope.userLocation": true
             * }
             */
          }
        })
      }else{
       shouquan()
      }
    }
  })
}
module.exports = {
  formatTime: formatTime,
  shouquan :shouquan,
  globalData: {
    userInfo: null,
  }
  
}


